package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserInfoDao;
import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import com.itheima.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:UserInfoServiceImpl
 * Package:com.itheima.service.impl
 * Description:
 * Date:2020/3/11 16:33
 * User: snow
 */
@Service("userService")
@Transactional
public class UserInfoServiceImpl implements UserInfoService {

	@Autowired
	private UserInfoDao userInfoDao;
	@Autowired
	private BCryptPasswordEncoder bCryptPasswordEncoder;

	/**
	 * 权限管理
	 *
	 * @param username
	 * @return
	 * @throws UsernameNotFoundException
	 */
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		UserInfo userInfo = null;
		try {
			userInfo = userInfoDao.findByUserName(username);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 处理自己的用户对象封装成UserDetails
		User user = new User(userInfo.getUserName(), userInfo.getUserPassword(),
				userInfo.getUserStatus() == 0 ? false : true, true, true,
				true, getAuthority(userInfo.getRoles()));
		return user;
	}

	// 返回一个list集合，集合中装入的是角色描述
	public List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {

		List<SimpleGrantedAuthority> list = new ArrayList<>();
		for (Role role : roles) {
			list.add(new SimpleGrantedAuthority(role.getRoleName()));
		}
		return list;
	}

	/**
	 * 查找所有
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserInfo> findAll(int page,int size) throws Exception {
		/**
		 * //参数配置
		 * pageNum 页码值   pageSize  每页最大值
		 */
		PageHelper.startPage(page,size);
		return userInfoDao.findAll();
	}
	/**
	 * 查找所有
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserInfo> findAll() throws Exception {
		return userInfoDao.findAll();
	}

	/**
	 * 添加（保存）
	 *
	 * @param userInfo
	 * @throws Exception
	 */
	@Override
	public void save(UserInfo userInfo) throws Exception {
		// 对密码加密处理
		userInfo.setUserPassword(bCryptPasswordEncoder.encode(userInfo.getUserPassword()));
		userInfoDao.save(userInfo);
	}

	@Override
	public UserInfo findById(String userId) throws Exception {
		return userInfoDao.findById(userId);
	}

	/**
	 * 查找用户可添加角色
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Role> findOtherRole(String id) throws Exception {
		return userInfoDao.findOtherRole(id);
	}

	/**
	 * 给用户添加角色
	 * @param userId
	 * @param roleIds
	 * @throws Exception
	 */
	@Override
	public void addRoleToUser(String userId, String[] roleIds) throws Exception {
		//遍历传参
		for (String roleId : roleIds) {
			userInfoDao.addRoleToUser(userId,roleId);
		}
	}

	/**
	 * 通过名字获取user信息
	 * @param userName
	 * @return
	 */
	@Override
	public UserInfo findByName(String userName) throws Exception{
		return userInfoDao.findByUserName(userName);
	}

	@Override
	public UserInfo findUserInfoById(String userInfoId) {
		return userInfoDao.findUserInfoById(userInfoId);
	}

	@Override
	public void freeze(String id) {
		userInfoDao.freeze(id);
	}

	@Override
	public void open(String id) {
		userInfoDao.open(id);
	}


}
